# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from scrapy.exceptions import DropItem
import requests


class DailispiderPipeline(object):
    def process_item(self, item, spider):
        return item


# 在保存之前检查
class CheckIpPipeline(object):
    def process_item(self, item, spider):
        proxy = {
            'http': f"http://{item['ip']}:{item['ip_port']}",
        }
        text = None
        try:
            response = requests.get(url='http://www.baidu.com', proxies=proxy)
            text = response.text
        except Exception as e:
            print('代理不可用：', e)
        finally:
            if text:
                return item
            else:
                # 不处理的Item
                raise DropItem('item has no effect, do nothing')
